//index.js
//获取应用实例
const app = getApp()
wx.cloud.init();
var checkResult = function (result) {
  if (!result || result.code !== 20000) {
    console.log('error' + result.code);
    wx.showToast({
      icon: 'none',
      title: '没有数据或者操作失败，请刷新尝试',
    });
    return false;
  }
  return true;
};
Page({
  data: {
    list: [{
      "text": "概览",
      "iconPath": "/assets/tongji.png",
      "selectedIconPath": "/assets/tongji.png"
    },
    {
      "text": "控制",
      "iconPath": "/assets/yaokong.png",
      "selectedIconPath": "/assets/yaokong.png"
    },
    {
      "text": "选手",
      "iconPath": "/assets/xuanshou.png",
      "selectedIconPath": "/assets/xuanshou.png"
    }]
  },
  tabChange(e) {
    console.log('tab change', e);
    this.setData({
      router: e.detail.index
    });
  },
  onLoad: function (options) {
    if (!options.sid) {
      wx.redirectTo({
        url: '../index/index',
      });
      return;
    }
    var router = options.router;
    if(!router){
      router = 0;
    }
    this.setData({
      router: router
    });
    var sid = options.sid;
    var that = this;
    //获取比赛信息
    wx.cloud.callFunction({
      // 云函数名称
      name: 'admin',
      // 传给云函数的参数
      data: {
        action: 'contests',
        sid: parseInt(sid)
      },
      success: function (res) {
        console.log(res);
        var result = res.result;
        if (!checkResult(result)) {
          return;
        }
        var contest = result.data[0];
        contest.weburl = app.globalData.webprefix + 'index.html?sid=' + contest.sid;
        that.setData({
          contest: contest
        });
        wx.setNavigationBarTitle({
          title: contest.name,
        });
        //获取选手信息
        wx.cloud.callFunction({
          // 云函数名称
          name: 'admin',
          // 传给云函数的参数
          data: {
            action: 'players',
            contestId: contest._id
          },
          success: function (res) {
            console.log('player res: ', res);
            var result = res.result;
            if (!checkResult(result)) {
              return;
            }
            var players = result.data;
            that.setData({
              players: players
            });
          },
          fail: err => {
            console.log(err);
            wx.showToast({
              icon: 'none',
              title: '加载失败，请尝试刷新',
              duration: 2000
            })
          }
        });
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          icon: 'none',
          title: '加载失败，请尝试刷新',
          duration: 2000
        })
      }
    });
  },

  screenGoto: function(screenjob){
    if(!screenjob){
      return;
    }
    //控制屏幕
    wx.cloud.callFunction({
      // 云函数名称
      name: 'admin',
      // 传给云函数的参数
      data: {
        action: 'savecontest',
        screenjob: screenjob,
        id: this.data.contest._id
      },
      success: function (res) {
        console.log(res);
        var result = res.result;
        if (!result || result.code !== 20400) {
          console.log('error' + result.code);
          wx.showToast({
            icon: 'none',
            title: '操作失败，请刷新尝试',
          });
          return;
        }
        wx.showToast({
          icon: 'none',
          title: '操作成功',
        });
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          icon: 'none',
          title: '加载失败，请尝试刷新',
        })
      }
    });
  },
  screenGotoTitle: function(){
    this.screenGoto('title');
  },
  screenGotoRank: function(){
    this.screenGoto('rank');
  },
  screenGotoSign: function(){
    this.screenGoto('sign');
  },
  gotoEditContest: function(){
    if(!this.data.contest || !this.data.contest.sid){
      return;
    }
    wx.navigateTo({
      url: '../contest-edit/contest-edit?sid=' + this.data.contest.sid,
    });
  },
  onShareAppMessage: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  },
  onShareTimeline: function (res) {
    return {
      title: '文艺比赛小行家',
      path: '/pages/index/index',
      imageUrl: '../../assets/singer.png'
    }
  }

})
